package com.jet.merchant.controller;

import com.jet.merchant.domain.dto.RoleDto;
import com.jet.merchant.domain.entity.ClerkRole;
import com.jet.merchant.domain.query.RolePageQuery;
import com.jet.merchant.domain.vo.RoleVo;
import com.jet.merchant.service.IClerkRoleService;
import com.jet.core.model.vo.PageVo;
import com.jet.core.result.ResultJson;
import org.springframework.web.bind.annotation.*;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/clerk/role")
public class RoleController {

    private final IClerkRoleService clerkRoleService;

    public RoleController(IClerkRoleService clerkRoleService) {
        this.clerkRoleService = clerkRoleService;
    }

    @PostMapping
    public ResultJson<Void> create(@RequestHeader long merchantNo,
                                   @RequestHeader long storeNo,
                                   @RequestHeader String accountId,
                                   @RequestBody RoleDto dto) {
        clerkRoleService.save(merchantNo, dto);
        return ResultJson.ok();
    }

    @PutMapping(value = "/{id}")
    public ResultJson<Void> edit(@RequestHeader long merchantNo,
                                 @RequestHeader long storeNo,
                                 @RequestHeader String accountId,
                                 @PathVariable("id") String id,
                                 @RequestBody RoleDto dto) {
        clerkRoleService.edit(merchantNo, id, dto);
        return ResultJson.ok();
    }

    @PutMapping("/status/{id}")
    public ResultJson<Void> editStatus(@RequestHeader long merchantNo,
                                       @RequestHeader long storeNo,
                                       @RequestHeader String accountId,
                                       @PathVariable("id") String id,
                                       @RequestParam int status) {
        clerkRoleService.editStatus(merchantNo, id, status);
        return ResultJson.ok();
    }

    @DeleteMapping("/{id}")
    public ResultJson<Void> delete(@RequestHeader long merchantNo,
                                   @RequestHeader long storeNo,
                                   @RequestHeader String accountId,
                                   @PathVariable("id") String id) {
        clerkRoleService.delete(merchantNo, id);
        return ResultJson.ok();
    }

    @GetMapping("/{id}")
    public ResultJson<ClerkRole> info(@RequestHeader long merchantNo,
                                       @RequestHeader long storeNo,
                                       @RequestHeader String accountId,
                                       @PathVariable("id") String id) {
        ClerkRole vo = clerkRoleService.getById(id);
        return ResultJson.ok(vo);
    }

    @GetMapping("/page")
    public ResultJson<PageVo<RoleVo>> page(@RequestHeader long merchantNo,
                                           @RequestHeader long storeNo,
                                           @RequestHeader String accountId,
                                           RolePageQuery query) {
        PageVo<RoleVo> vo = clerkRoleService.page(merchantNo, query);
        return ResultJson.ok(vo);
    }
}
